package controller

import (
	"hft/Database/admin"
	"hft/Http/Controller/loupan/loupan_model"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type LouPanSwiper struct{}

func (*LouPanSwiper) CreateSwiper(c *gin.Context) {
	var data *admin.LouPanSwiper

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.LouPanSwiperService.CreateLouPanSwiper(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*LouPanSwiper) EditSwiper(c *gin.Context) {
	var data *admin.LouPanSwiper

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.LouPanSwiperService.EditLouPanSwiper(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*LouPanSwiper) GetLouPanSwiperList(c *gin.Context) {
	var paginate request.LouPanSwiperSearch

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.LouPanSwiperService.GetSwiperList(claims.BaseClaims, &paginate)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*LouPanSwiper) Delete(c *gin.Context) {
	var data *loupan_model.DeleteLouSwiper
	c.ShouldBindJSON(&data)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	err = services.LouPanSwiperService.Delete(int(data.LouID), data.IDS, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

var LouPanSwiperController = new(LouPanSwiper)
